//
//  PersonalCenterVC.m
//  CreditHub
//
//  Created by chenglang on 2024/9/21.
//

#import "PersonalCenterVC.h"
#import "PersonalCenterItemView.h"
#import "HomeRequest.h"
#import "BankPopupViewController.h"

@interface PersonalCenterVC ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *imageTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelDetailTitle;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutHeight;
@property (nonatomic , strong) NSMutableArray *arraySuchan;

@property (weak, nonatomic) IBOutlet UILabel *labelCerTitle;
@property (weak, nonatomic) IBOutlet UIImageView *imageCerOK;
@property (weak, nonatomic) IBOutlet UIImageView *imageCerRight;

@end

@implementation PersonalCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.ch_title = @"Me";
    
    self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [self.scrollView.mj_header setValue:@(YES) forKeyPath:@"stateLabel.hidden"];
    [self.scrollView.mj_header setValue:@(YES) forKeyPath:@"lastUpdatedTimeLabel.hidden"];
    
}
- (void)headerRefresh{
    [self requestInfo];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self requestInfo];
    
}

- (void)requestInfo{
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestPersonalCenterSuccess:^(NSURLSessionDataTask *task, id response) {
        [weakSelf.scrollView.mj_header endRefreshing];
        if([response[@"ofexcitement"] integerValue] == 0){
            BOOL isCertified = [response[@"pity"][@"isCertified"] boolValue];
            if(isCertified == 1){
                weakSelf.imageCerOK.hidden = NO;
                weakSelf.imageCerRight.hidden = YES;
                weakSelf.labelCerTitle.text = @"Certified";
            }else{
                weakSelf.imageCerOK.hidden = YES;
                weakSelf.imageCerRight.hidden = NO;
                weakSelf.labelCerTitle.text = @"Not certified >>>";
            }

            NSDictionary *dicpour = response[@"pity"][@"pour"];
            weakSelf.labelTitle.text = dicpour[@"withme"];
            weakSelf.labelDetailTitle.text = dicpour[@"sleep"];
            weakSelf.arraySuchan = [NSMutableArray arrayWithArray:response[@"pity"][@"tentacles"]];
            
            NSArray *arr =  weakSelf.contentView.subviews;
            NSInteger count = arr.count;
            if(count< weakSelf.arraySuchan.count){
                count =  weakSelf.arraySuchan.count;
            }
            
            for(int i = 0 ; i < count;i++){
                
                PersonalCenterItemView *view;
                if(arr.count>i){
                    view = arr[i];
                    if(weakSelf.arraySuchan.count>i){
                        view.dicInfo = [NSMutableDictionary dictionaryWithDictionary:weakSelf.arraySuchan[i]];
                    }else{
                        [view removeFromSuperview];
                    }
                }else{
                    view = [[NSBundle mainBundle] loadNibNamed:@"PersonalCenterItemView" owner:self options:@{}].firstObject;
                    view.dicInfo = [NSMutableDictionary dictionaryWithDictionary:weakSelf.arraySuchan[i]];
                    view.tag = 2000+i;
                    [weakSelf.contentView addSubview:view];
                    [view mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.mas_equalTo(15);
                        make.right.mas_equalTo(-15);
                        make.height.offset(84);
                        make.top.mas_equalTo(i*84);
                    }];
                }
                
              
            }
            weakSelf.layoutHeight.constant =  weakSelf.arraySuchan.count*84;
            if([response[@"pity"][@"yousat"] isKindOfClass:[NSDictionary class]]){
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:response[@"pity"][@"yousat"] forKey:@"AccountCancellation"];
                [defaults synchronize];
            }
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [weakSelf.scrollView.mj_header endRefreshing];
    }];
}
- (IBAction)clickCer:(id)sender {
    if(self.imageCerOK.hidden){
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationBackHomeKey object:@{@"index":@(0)}];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
